/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.component.controls;

import cn.ac.nercis.pes.community.support.event.EventCallback;
import cn.ac.nercis.pes.community.utils.IconUtils;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * 依据规范选项
 * @author zpy
 * @version 1.0
 */
public class PesBasisOption extends HBox {
    private final StringProperty text = new SimpleStringProperty("");
    private final BooleanProperty selected = new SimpleBooleanProperty(false);

    private CheckBox checkTag;
    private TextField nameTxt;
    private Label delLabel;

    @Getter
    @Setter
    private EventCallback<PesBasisOption> onModifyEvent;

    @Getter
    @Setter
    private EventCallback<PesBasisOption> onDeleteEvent;

    @Getter
    @Setter
    private Boolean isAdd;

    public PesBasisOption(){
        this.getStyleClass().add("basis-option");
        this.initialize();
        this.setSpacing(10);
    }

    public final StringProperty textProperty() { return text; }
    public final String getText() { return nameTxt.getText(); }
    public final void setText(String value) { nameTxt.setText(value); }

    public final BooleanProperty selectedProperty() { return selected; }
    public final Boolean getSelected() { return checkTag.isSelected(); }
    public final void setSelected(Boolean value) { checkTag.setSelected(value); }

    private void initialize(){
        checkTag = new CheckBox();
        checkTag.setSelected(selected.get());
        checkTag.selectedProperty().addListener((observableValue, oldValue, newValue) -> {
            if(!Objects.equals(oldValue,newValue) && Objects.nonNull(onModifyEvent)){
                onModifyEvent.action(this);
            }
        });
        var lblBox = new HBox();
        lblBox.setSpacing(10);
        lblBox.getStyleClass().add("basis-option-label");
        nameTxt = new TextField();
        nameTxt.setText(text.getValue());
        nameTxt.getStyleClass().add("pes-option-text-field");
        nameTxt.managedProperty().bind(nameTxt.visibleProperty());
        nameTxt.textProperty().addListener((observableValue, oldValue, newValue) -> {
            if(!StringUtils.equals(oldValue,newValue) && Objects.nonNull(onModifyEvent)){
                onModifyEvent.action(this);
            }
        });
        HBox.setHgrow(nameTxt, Priority.SOMETIMES);

        delLabel = new Label();
        delLabel.getStyleClass().add("del-picture-btn");
        delLabel.setTooltip(new Tooltip("删除"));
        delLabel.setGraphic(IconUtils.getGlyph("del-picture-icon", "del-picture-icon"));
        delLabel.setOnMouseClicked((me) -> {
            if (Objects.nonNull(onDeleteEvent)) {
                onDeleteEvent.action(this);
            }
        });
        lblBox.getChildren().addAll(nameTxt,delLabel);

        HBox.setHgrow(lblBox, Priority.SOMETIMES);
        this.getChildren().addAll(checkTag,lblBox);
    }

}